/**
 * 后台公共JS
 */

/**
 * JQ操作
 */
$(document).ready(function() {
    SyntaxHighlighter.config.clipboardSwf = '/Component/syntax/scripts/clipboard.swf';
    SyntaxHighlighter.all();

    /**
     * 删除上传的图片
     */
    $(".clearUploadImg").live("click", function() {
        //获取被删除的元素cookie名称
        dataName = $(this).attr("data");
        deleteArray = getCookie(dataName).split(",");
        for (key in deleteArray) {
            if (deleteArray[key] == $(this).prev("input").val()) {
                deleteArray.splice(key, 1);
            }
        }
        setCookie(dataName, deleteArray);
        $(this).parent("div").remove();
    });
    
    /**
     * 分类选择URL时显示的结果
     */
    if($(".c_model_id").val() == '-1'){
        $(".c_url").show();
    }
    $(".c_model_id").change(function(){
        if($(this).val() == '-1'){
            $(".c_url").show();
        }else{
            $(".c_url").hide();
        }
    })
    

});

/**
 * 编辑器操作
 */
KindEditor.ready(function(K) {
    var options = {
        cssPath: '/Component/editor/kindeditor/plugins/code/prettify.css',
        filterMode: false
    };
    var editor = K.create('#editor_id', options);
});

/**
 * 添加系统菜单
 * @returns {undefined}
 */
function addSystemMenu() {
    var dialog = art.dialog({
        id: 'dialogEdit',
        title: '添加菜单',
        content: document.getElementById('editMenu'),
        width: 500,
        lock: true,
        fixed: true,
        ok: function() {
            formParam = [];
            formParam['pid'] = selectedValue('parent');
            formParam['value'] = inputValue('value');
            formParam['name'] = inputValue('name');
            formParam['url'] = inputValue('url');
            formParam['listsort'] = inputValue('listsort');
            result = ajax('/index.php?g=Admin&m=Index&a=addMenu', 'post', formParam);
            if (result == false) {
                return false;
            } else {
                alert(result.info);
                reload();
                return true;
            }

        },
        okValue: '添加',
        cancelValue: '取消',
        cancel: function() {
        }
    });
}

/**
 * 编辑系统菜单
 * @param {str} 菜单值
 */
function editSystemMenu(id) {

    data = new Array();
    data['id'] = id;
    menu = ajax('/?g=Admin&m=Index&a=editMenu', 'post', data);
    if (menu == false) {
        return false;
    }
    $("select[name=parent] option").each(function() {
        if ($(this).val() == menu.info.pid) {
            $(this).attr("selected", "selected");
        }
    })
    inputValue('value', menu.info.value);
    inputValue('name', menu.info.name);
    if (menu.info.pid > 0) {
        inputValue('url', menu.info.url);
    } else {
        inputValue('url', '');
    }
    inputValue('listsort', menu.info.listsort);
    var dialog = art.dialog({
        id: 'dialogEdit',
        title: '编辑菜单',
        content: document.getElementById('editMenu'),
        width: 500,
        lock: true,
        fixed: true,
        ok: function() {
            formParam = [];
            formParam['pid'] = selectedValue('parent');
            formParam['value'] = inputValue('value');
            formParam['name'] = inputValue('name');
            formParam['url'] = inputValue('url');
            formParam['listsort'] = inputValue('listsort');
            formParam['id'] = id;
            result = ajax('/index.php?g=Admin&m=Index&a=updateMenu', 'post', formParam);
            if (result == false) {
                return false;
            } else {
                alert(result.info);
                reload();
                return true;
            }

        },
        okValue: '编辑',
        cancelValue: '取消',
        cancel: function() {
        }
    });
}

/**
 * 删除系统菜单
 * @param {str} 菜单值
 */
function deleteSystemMenu(id) {
    var dialog = art.dialog({
        id: 'dialogEdit',
        title: '删除菜单',
        content: '确认要删除吗？',
        width: 100,
        lock: true,
        fixed: true,
        ok: function() {
            formParam = [];
            formParam['id'] = id;
            result = ajax('/index.php?g=Admin&m=Index&a=deleteMenu', 'post', formParam);
            if (result == false) {
                return false;
            } else {
                alert(result.info);
                reload();
                return true;
            }

        },
        okValue: '删除',
        cancelValue: '取消',
        cancel: function() {
        }
    });
}

/**
 * 添加模型
 */
function addModel() {
    var dialog = art.dialog({
        id: 'dialogEdit',
        title: '添加模型',
        content: document.getElementById('editModel'),
        width: 500,
        lock: true,
        fixed: true,
        ok: function() {
            formParam = [];
            formParam['model_name'] = inputValue('model_name');
            formParam['name'] = inputValue('name');
            formParam['listsort'] = inputValue('listsort');
            result = ajax('/index.php?g=Admin&m=Model&a=addModel', 'post', formParam);
            if (result == false) {
                return false;
            } else {
                alert(result.info);
                reload();
                return true;
            }

        },
        okValue: '添加',
        cancelValue: '取消',
        cancel: function() {
        }
    });
}

function deleteWarning(url) {
    art.dialog({
        title: '您确定要删除我吗？',
        content: '您真的舍得删除我吗？',
        lock: true,
        okValue: '确定删除',
        cancelValue: '取消',
        ok: function() {
            jump(url.href);
        },
        cancel: function() {
        }
    });
    return false;
}